package chatclient_01;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicButtonUI;

public class ButtonTabComponent extends JPanel implements ActionListener {

  private static final long serialVersionUID = 1L;
  private final JTabbedPane pane;
  private JLabel label;
  private static Font bold = new Font("Tahoma", 1, 11);
  private static Font normal = new Font("Tahoma", 0, 11);
  private static final MouseListener buttonMouseListener = new MouseAdapter() {

    @Override
    public void mouseEntered(MouseEvent e) {
      Component component = e.getComponent();
      if ((component instanceof AbstractButton)) {
        AbstractButton button = (AbstractButton) component;
        button.setBorderPainted(true);
      }
    }

    @Override
    public void mouseExited(MouseEvent e) {
      Component component = e.getComponent();
      if ((component instanceof AbstractButton)) {
        AbstractButton button = (AbstractButton) component;
        button.setBorderPainted(false);
      }
    }
  };

  public ButtonTabComponent(JTabbedPane pane, boolean closable) {
    super(new FlowLayout(0, 0, 0));
    if (pane == null) {
      throw new NullPointerException("TabbedPane is null");
    }
    this.pane = pane;
    setOpaque(false);

    this.label = new JLabel() {

      private static final long serialVersionUID = 1L;

      @Override
      public String getText() {
        int i = ButtonTabComponent.this.pane.indexOfTabComponent(ButtonTabComponent.this);
        if (i != -1) {
          return ButtonTabComponent.this.pane.getTitleAt(i);
        }
        return null;
      }
    };
    add(this.label);

    this.label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));

    if (closable) {
      JButton button = new TabButton();
      add(button);
    }

    setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
  }

  public void setBold(boolean b) {
    if (b) {
      this.label.setFont(bold);
    } else {
      this.label.setFont(normal);
    }
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    int i = this.pane.indexOfTabComponent(ButtonTabComponent.this);
    if (i != -1) {
      this.pane.remove(i);
    }
  }

  private class TabButton extends JButton {

    private static final long serialVersionUID = 1L;

    TabButton() {
      int size = 15;
      setPreferredSize(new Dimension(size, size));
      setToolTipText("close this tab");

      setUI(new BasicButtonUI());

      setContentAreaFilled(false);

      setFocusable(false);
      setBorder(BorderFactory.createEtchedBorder());
      setBorderPainted(false);

      addMouseListener(ButtonTabComponent.buttonMouseListener);
      setRolloverEnabled(true);

      addActionListener(ButtonTabComponent.this);
    }

    @Override
    public void updateUI() {
    }

    @Override
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g.create();

      if (getModel().isPressed()) {
        g2.translate(1, 1);
      }
      g2.setStroke(new BasicStroke(1.0F));
      g2.setColor(Color.BLACK);
      if (getModel().isRollover()) {
        g2.setColor(Color.RED);
      }
      int delta = 5;
      g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
      g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
      g2.dispose();
    }
  }
}